package cn.maihe.elg.operation.institution.service;

import cn.maihe.elg.operation.model.dto.BaseResp;
import cn.maihe.elg.operation.common.TradeContext;
import cn.maihe.elg.operation.repository.entity.ApplyOrderInfoDO;
import cn.maihe.elg.operation.model.dto.institution.NotifyOrderReqDTO;
import cn.maihe.elg.operation.institution.BaseInstitutionOpenService;
import cn.maihe.elg.operation.institution.InstitutionException;
import cn.maihe.framework.starter.mybatis.dao.MybatisDaoImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * @Description
 * @Author WJH
 * @Date 2021/04/27
 */
@Service
public class ZhtNotifyOrderStatus extends BaseInstitutionOpenService<NotifyOrderReqDTO, BaseResp> {

    @Override
    protected BaseResp processBusiness(TradeContext<NotifyOrderReqDTO, BaseResp> context) {
        NotifyOrderReqDTO req = context.getReq();
        ApplyOrderInfoDO dbDo = MybatisDaoImpl.run().selectById(ApplyOrderInfoDO.class, req.getApplyOrderId());
        Assert.notNull(dbDo, () -> "查无此申请订单");

        if (!req.getApplyOrderNo().equals(dbDo.getApplyOrderNo()) || req.getSourceStatus().getStatus() != dbDo.getStatus().intValue()) {
            throw InstitutionException.of("申请订单不匹配或订单状态不匹配", null);
        }

        ApplyOrderInfoDO updateDO = ApplyOrderInfoDO.builder().id(req.getApplyOrderId())
                .status(req.getTargetStatus().getStatus()).build();
        MybatisDaoImpl.run().updateById(updateDO);

        return new BaseResp();
    }

}
